const appID = 'wxecc9322469a3f41b';
const redirectURI = encodeURIComponent('your_redirect_uri'); // 需要进行 URL 编码
const secret = '7803a7c5261cd59ce2fcc64b7d212b4f'
const scope = 'snsapi_base'; // 或 'snsapi_userinfo'
const authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appID}&redirect_uri=${redirectURI}&response_type=code&scope=${scope}&state=STATE#wechat_redirect`;

  
// openid.js
async function getOpenid(req, res) {
    const code = req.query.code;
  
    // 使用code换取access_token和openid
    const response = await fetch(`https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appID}&secret=${secret}&code=${code}&grant_type=authorization_code`);
    const data = await response.json();
  
    const openid = data.openid;
    res.send(`用户的openid: ${openid}`);
}
  
  module.exports = getOpenid; // 导出接口逻辑函数